package com.demo.webapp.action.root;

import java.util.ArrayList;
import java.util.List;

import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Function;
import com.demo.core.model.Site;
import com.demo.core.service.base.FunctionService;
import com.demo.core.util.collection.JsonUtils;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * 功能Action 多站点的功能由Function模型进行定义 但每个站点只能管理本站点组的功能
 * 
 * @author ryuu.kk
 * 
 */
public class FunctionAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8332054592988418411L;

	/**
	 * 主键ID
	 */
	private Long id;

	/**
	 * 父主键ID
	 */
	private Long pid;

	/**
	 * 站点ID
	 */
	private Long siteId;

	/**
	 * 功能树的json字符串
	 */
	private String json;
	
	/**
	 * 显示类型:(列表list) or (树tree)
	 */
	private String type;

	/**
	 * 功能模型(树结构)
	 */
	private Function tree;

	/**
	 * 功能模型
	 */
	private Function function;

	/**
	 * 站点功能模型列表
	 */
	private List<Function> functionList;

	/**
	 * 站点功能服务
	 */
	private FunctionService functionService;

	/**
	 * 功能模型
	 */
	@com.demo.security.annotation.function.Function(id = 101101, pid = 101100, code = "manage.function.execute")
	public String execute() {

		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		// 站点组ID
		Long siteGroupId = site.getSiteGroupId();

		this.function = functionService.tree(this.id, siteGroupId);

		// 本站点分配的站点功能(List)
		this.functionList = functionService.query(siteId);

		return SUCCESS;
	}

	/**
	 * 功能列表
	 * 
	 * @return String
	 */
	public String list() {

		SearchArgumentSupport args = getArgumentSupport();
		ResultSearchResult<Function> result = null;
		// 得到站点分组类型ID
		Long siteGroupId = getSite().getSiteGroupId();
		// 本站点分配的站点功能(List)
		result = functionService.search(siteGroupId, args);
		this.setResultSupport(result);
		this.functionList = new ArrayList<Function>(result.getResults());
		
		return SUCCESS;
	}
	
	/**
	 * 功能创建
	 * @return String 
	 */
	public String create() {
		
		return SUCCESS;
	}
	
	/**
	 * 创建站点功能信息
	 * 
	 * @return String
	 */
	public String save() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		// 站点组ID
		Long siteGroupId = site.getSiteGroupId();

		function.setSiteGroupId(siteGroupId);
		// function.setType(type)
		id = functionService.save(function);
		return CALLBACK;
	}
	
	/**
	 * 功能编辑
	 * @return String 
	 */
	public String edit() {
		if (id != null) {
			this.function = functionService.find(id);
		}
		return SUCCESS;
	}
	
	/**
	 * 功能更新
	 * @return String 
	 */
	public String update() {
		// 取得功能对象ID
		// 更新功能模型
		functionService.update(id, function);
		addActionMessage(getText("function.updated", "功能已更新", function.getName()));
		return CALLBACK;
	}
	
	/**
	 * 删除功能信息
	 * 
	 * @return String
	 */
	public String delete() {

		functionService.delete(id);
		return CALLBACK;
	}
	
	/**
	 * 功能展示
	 * 
	 * @return String 
	 */
	public String view() {
		if (id != null) {
			this.function = functionService.find(id);
		}
		
		return SUCCESS;
	}

	/**
	 * 功能树
	 * 
	 * @return String
	 */
	public String tree() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		// 站点组ID
		Long siteGroupId = site.getSiteGroupId();
		// 站点最大功能集合(Tree)
		this.tree = functionService.tree(id, siteGroupId);
		// 生成json树
		json = JsonUtils.zTree(tree, 2);
		return SUCCESS;
	}

	// ~~ settings here
	// ---------------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public Long getSiteId() {
		return siteId;
	}

	public String getJson() {
		return json;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Function getTree() {
		return tree;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}
}
